<template>
    <el-row type="flex" class="row-bg" justify="center">
<!--        <el-col :xl="6" :lg="7">-->
            <div class="grid-content"  >
              <div class="content-center">
              <img style="display: block"
                        src="../assets/img/Logo.png"/>
              <h1 style="font-family: '华文楷体'">医疗器械唯一标识管理平台</h1>
              </div>
            </div>
<!--        </el-col>-->
<!--        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>-->
          <div class="login-form">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="auto"  class="demo-loginForm" size="large">
              <el-form-item label="组织编码" prop="organizationCode" >
                <el-input v-model="loginForm.organizationCode" style="width: 100%; float: left"></el-input>
              </el-form-item>
              <el-form-item label="账户" prop="username" style="width: 100%">
                    <el-input v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="userPassword" style="width: 100%">
                    <el-input v-model="loginForm.userPassword" type="password"></el-input>
                </el-form-item>
              <el-form-item size="mini">
              <el-link :underline="false" href="../apply" style=" float: right">提交申请</el-link>
              </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('loginForm')">立即登录</el-button>
                </el-form-item>
            </el-form>
          </div>
    </el-row>
</template>

<script>
    import qs from 'qs'
    export default {
        name:"Login",
        data() {
            return {
                //表单数据
                loginForm: {
                    username: '',
                    userPassword: '',
                  organizationCode: '',
                    token: ''
                },
                //前台输入框的校验
                rules: {
                  organizationCode: [
                    { required: true, message: '请输入组织编码', trigger: 'change' },
                    { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
                  ],
                    username: [
                        { required: true, message: '请输入账户', trigger: 'blur' }
                    ],
                    userPassword: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],

                },
                captchaImg:null
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/login',this.loginForm).then(res =>{
                            //获取 jwt : authorization
                            const jwt = res.headers['authorization']
                            //把 jwt 存储在store
                            this.$store.commit('SET_TOKEN',jwt)
                            //登录成功路由跳转到主页
                            this.$router.push('/index')
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //从后台获取验证码
            getCaptchaImg(){
                this.$axios.get('/captcha').then(res => {
                    //随机码
                    this.loginForm.token = res.data.data.token
                    //验证码
                    this.captchaImg = res.data.data.captchaImg
                })
            }
            //Result - code、msg、data
        },
        //页面初始化时调用获取验证码方法
        created() {
            this.getCaptchaImg()
        }
    }
</script>

<style scoped>
    #app{
        height: 100%;
        margin: 0px;
        padding: 0px;
    }
    .row-bg{
      width: 100%;
      height: 100%;
    }
    .grid-content{
      width: 50%;
      height: 100%;
      background-color: #FFFFFF;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .content-center{
      width: 60%;
      height: 60%;
      margin-top: 5%;
      margin-left: 20%;
    }
    .login-form{
      width: 50%;
      height: 100%;
      background-color: #EEEEEE;
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
    }
    .demo-loginForm{
      width: 50%;
      height: 50%;
      margin-top: 10%;


    }
    /*.grid-center{
    }*/
    /*.el-col {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        text-align: center;
    }*/
    /* .el-row {
         height: 100%;
         background-color: #fafafa;
     }
     .el-divider {
         height: 200px;
     }
     .captchaImg {
         float: left;
         margin-left: 8px;
         border-radius: 4px;
     }
     .demo-loginForm{
       background-color: darkgrey;
     }*/
</style>
